﻿namespace CKFinder.Connector.CommandHandlers
{
    using CKFinder.Connector;
    using System;
    using System.Text;
    using System.Web;
    using System.Xml;

    internal abstract class XmlCommandHandlerBase : CommandHandlerBase
    {
        private XmlNode _ConnectorNode;
        private XmlDocument _Xml;

        protected abstract void BuildXml();
        protected virtual bool MustCheckRequest()
        {
            return true;
        }

        protected virtual bool MustIncludeCurrentFolder()
        {
            return true;
        }

        public override void SendResponse(HttpResponse response)
        {
            response.ClearHeaders();
            response.Clear();
            response.CacheControl = "no-cache";
            response.ContentEncoding = Encoding.UTF8;
            response.ContentType = "text/xml";
            this._Xml = new XmlDocument();
            this._Xml.AppendChild(this._Xml.CreateXmlDeclaration("1.0", "utf-8", null));
            this._ConnectorNode = XmlUtil.AppendElement(this._Xml, "Connector");
            XmlNode node = XmlUtil.AppendElement(this.ConnectorNode, "Error");
            try
            {
                base.CheckConnector();
                if (this.MustCheckRequest())
                {
                    base.CheckRequest();
                }
                if (base.CurrentFolder.ResourceTypeName.Length > 0)
                {
                    XmlUtil.SetAttribute(this._ConnectorNode, "resourceType", base.CurrentFolder.ResourceTypeName);
                }
                if (this.MustIncludeCurrentFolder())
                {
                    XmlNode node2 = XmlUtil.AppendElement(this._ConnectorNode, "CurrentFolder");
                    XmlUtil.SetAttribute(node2, "path", base.CurrentFolder.ClientPath);
                    try
                    {
                        XmlUtil.SetAttribute(node2, "url", base.CurrentFolder.Url);
                    }
                    catch
                    {
                        XmlUtil.SetAttribute(node2, "url", "");
                    }
                    XmlUtil.SetAttribute(node2, "acl", base.CurrentFolder.AclMask.ToString());
                }
                this.BuildXml();
                XmlUtil.SetAttribute(node, "number", "0");
            }
            catch (ConnectorException exception)
            {
                XmlUtil.SetAttribute(node, "number", exception.Number.ToString());
            }
            catch
            {
                XmlUtil.SetAttribute(node, "number", 110.ToString());
            }
            response.Write(this._Xml.OuterXml);
            response.End();
        }

        protected XmlNode ConnectorNode
        {
            get
            {
                return this._ConnectorNode;
            }
        }

        protected XmlDocument Xml
        {
            get
            {
                return this._Xml;
            }
        }
    }
}

